package com.gx.smart.building.ui.spare.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.base.BaseViewModel
import com.gx.smart.building.SpareStockApiService
import com.gx.smart.building.SpareStockRecordApiService
import com.gx.smart.building.WarehouseManageApiService
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.warehouse.v1.WarehouseDto
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.warehouse.v1.WarehouseListQueryResp
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.SpareStockListQueryResp
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.SpareStockRecordResp
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareRecordDto
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareStockDto

/**
 *@author: xiaosy
 *时间: 2022/4/24
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class SparePartViewModel(app: Application) : BaseViewModel(app) {
    private val warehouseList: MutableLiveData<List<WarehouseDto>> = MutableLiveData()
    private val pareStockList: MutableLiveData<List<SpareStockDto>> = MutableLiveData()
    private val addSuccess: MutableLiveData<Boolean> = MutableLiveData()

    //查询仓库列表
    fun queryWarehouseList(currentPage: Int): LiveData<List<WarehouseDto>> {
        val comQuery = ComQuery
            .newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()
        WarehouseManageApiService.getInstance().queryWarehouseList(comQuery, object :
            CallBack<WarehouseListQueryResp>() {
            override fun callBack(result: WarehouseListQueryResp?) {
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    warehouseList.value = result.contentList
                } else {
                    handleServiceException(comResp)
                }
            }
        })
        return warehouseList
    }

    fun querySpareStockList(warehouseId: String): LiveData<List<SpareStockDto>> {
        SpareStockApiService.getInstance()
            .querySpareStockList(warehouseId, object : CallBack<SpareStockListQueryResp>() {
                override fun callBack(result: SpareStockListQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        pareStockList.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return pareStockList
    }

    fun querySpareStockList(keyword: String, warehouseId: String): LiveData<List<SpareStockDto>> {
        SpareStockApiService.getInstance()
            .querySpareStockList(
                keyword,
                warehouseId,
                object : CallBack<SpareStockListQueryResp>() {
                    override fun callBack(result: SpareStockListQueryResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            pareStockList.value = result.contentList
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return pareStockList
    }

    //库存记录新增(同：备件申请（APP）)
    fun insertSpareStockRecord(
        warehouseId: String,
        warehouseCode: String,
        warehouseName: String,
        recordTime: String,
        description: String,
        spareRecords: List<SpareRecordDto>
    ): LiveData<Boolean> {
        SpareStockRecordApiService.getInstance().insertSpareStockRecord(
            warehouseId,
            warehouseCode,
            warehouseName,
            recordTime,
            description,
            spareRecords,
            object : CallBack<SpareStockRecordResp>() {
                override fun callBack(result: SpareStockRecordResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        addSuccess.value = false
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        addSuccess.value = true
                    } else {
                        handleServiceException(comResp)
                        addSuccess.value = false
                    }
                }

            })
        return addSuccess
    }

}